const ncname = `[a-zA-Z_][\\-\\.0-9_a-zA-Z]*`;
const qnameCapture = `((?:${ncname}\\:)?${ncname})`;
const startTagOpen = new RegExp(`^<${qnameCapture}`);
const startTagClose = /^\s*(\/?)>/; 
const endTag = new RegExp(`^<\\/${qnameCapture}[^>]*>`);
const attribute = /^\s*([^\s"'<>\/=]+)(?:\s*(=)\s*(?:"([^"]*)"+|'([^']*)'+|([^\s"'=<>`]+)))?/; 
const defaultTagRE = /\{\{((?:.|\r?\n)+?)\}\}/g;

function parseHTML(html) {
    function advance(n) {
        html = html.substring(n);
    }

    function start(tagName, attrs) {

    }

    function end(tagName) {

    }

    function chars(text) {

    }

    while (html) {
        // 匹配 <
        let textEnd = html.indexOf('<');
        if(textEnd === 0) {
            const startTag = parseStartTag();
            if(startTag) {
                start(startTag.tagName, startTag.attrs);
                continue
            }
        }
        // 匹配 div id="app"
        let text;
        if(textEnd >= 0) {

        }
        // 匹配 >
    }
    // 解析开始标签
    function parseStartTag() {
        const matches = html.match(startTagOpen);
        if(matches) {
            const match = {
                tagName: matches[1],
                attrs: []
            }
            advance(matches[0].length);

            // 解析开始标签的属性
            let end, attr;
            while (!(end = html.match(startTagClose)) && (attr = html.match(attribute))) {
                match.attrs.push({
                    name: attr[1],
                    value: attr[3] || attr[4] || attr[5] || true
                })
                advance(attr[0].length);
            }

            // 删除尖角号 >
            if(end) {
                advance(end[0].length);
                return match;
            }
        }
    }
}

export function compileToFunctions(template) {
    let ast = parseHTML(template);
    console.log(ast);
}     